package vip.xiaonuo.biz.modular.detect.service.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.biz.modular.detect.entity.PythonDetectResult;
import vip.xiaonuo.biz.modular.detect.service.BizDetectService;

/**
 * @author
 * @date 2024/3/9、10:03
 */
@Component
@Slf4j
public class DetectResultListenner {
    private static final String TRUE = "True";
    private static final String FALSE = "False";
    @Autowired
    private BizDetectService detectService;

    @Autowired
    private DetectWebSocket detectWebSocket;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "detect.result" , durable = "true"),
            exchange = @Exchange(name = "jyl.detect"),
            key = "result"
    ))
    public void handleDetectResult(PythonDetectResult pythonDetectResult){
        String status = pythonDetectResult.getResult();
        log.info("接收到检测结果：{}", status);
        String path = pythonDetectResult.getPath();
        //BizDetectResult detectResult = new BizDetectResult();
        //detectResult.setStatus(status);
        //detectResult.setDetectedImageBase64(pythonDetectResult.getDetectedImageBase64());
        //String requestId = pythonDetectResult.getRequestId();
        //DetectWebSocket socket = DetectWebSocket.findWebSocketByRequestId(requestId);
        //if (socket != null) {
        //    socket.sendMessage(pythonDetectResult);
        //    DetectWebSocket.unregisterRequest(requestId);
        //}
        detectWebSocket.sendMessage(pythonDetectResult);
        //将检测结果与组织结构信息发送到消息队列中
        rabbitTemplate.convertAndSend("jyl.detect","info",pythonDetectResult);
        log.info("返回结果：{}", status);
        //detectService.fillResult(path,detectResult);
    }
}
